import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import classNames from 'classnames';
import * as React from 'react';
import Pagination from '../pagination';
import ListItem from './ListItem';
export const OmitProps = ['handleFilter', 'handleClear', 'checkedKeys'];
function parsePagination(pagination) {
  if (!pagination) {
    return null;
  }
  const defaultPagination = {
    pageSize: 10,
    simple: true,
    showSizeChanger: false,
    showLessItems: false
  };
  if (typeof pagination === 'object') {
    return Object.assign(Object.assign({}, defaultPagination), pagination);
  }
  return defaultPagination;
}
let ListBody = /*#__PURE__*/function (_React$Component) {
  _inherits(ListBody, _React$Component);
  var _super = _createSuper(ListBody);
  function ListBody() {
    var _this;
    _classCallCheck(this, ListBody);
    _this = _super.apply(this, arguments);
    _this.state = {
      current: 1
    };
    _this.onItemSelect = item => {
      const {
        onItemSelect,
        selectedKeys
      } = _this.props;
      const checked = selectedKeys.includes(item.key);
      onItemSelect(item.key, !checked);
    };
    _this.onItemRemove = item => {
      const {
        onItemRemove
      } = _this.props;
      onItemRemove === null || onItemRemove === void 0 ? void 0 : onItemRemove([item.key]);
    };
    _this.onPageChange = current => {
      _this.setState({
        current
      });
    };
    _this.getItems = () => {
      const {
        current
      } = _this.state;
      const {
        pagination,
        filteredRenderItems
      } = _this.props;
      const mergedPagination = parsePagination(pagination);
      let displayItems = filteredRenderItems;
      if (mergedPagination) {
        displayItems = filteredRenderItems.slice((current - 1) * mergedPagination.pageSize, current * mergedPagination.pageSize);
      }
      return displayItems;
    };
    return _this;
  }
  _createClass(ListBody, [{
    key: "render",
    value: function render() {
      const {
        current
      } = this.state;
      const {
        prefixCls,
        onScroll,
        filteredRenderItems,
        selectedKeys,
        disabled: globalDisabled,
        showRemove,
        pagination
      } = this.props;
      const mergedPagination = parsePagination(pagination);
      let paginationNode = null;
      if (mergedPagination) {
        paginationNode = /*#__PURE__*/React.createElement(Pagination, {
          simple: mergedPagination.simple,
          showSizeChanger: mergedPagination.showSizeChanger,
          showLessItems: mergedPagination.showLessItems,
          size: "small",
          disabled: globalDisabled,
          className: `${prefixCls}-pagination`,
          total: filteredRenderItems.length,
          pageSize: mergedPagination.pageSize,
          current: current,
          onChange: this.onPageChange
        });
      }
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("ul", {
        className: classNames(`${prefixCls}-content`, {
          [`${prefixCls}-content-show-remove`]: showRemove
        }),
        onScroll: onScroll
      }, this.getItems().map(_ref => {
        let {
          renderedEl,
          renderedText,
          item
        } = _ref;
        const {
          disabled
        } = item;
        const checked = selectedKeys.includes(item.key);
        return /*#__PURE__*/React.createElement(ListItem, {
          disabled: globalDisabled || disabled,
          key: item.key,
          item: item,
          renderedText: renderedText,
          renderedEl: renderedEl,
          checked: checked,
          prefixCls: prefixCls,
          onClick: this.onItemSelect,
          onRemove: this.onItemRemove,
          showRemove: showRemove
        });
      })), paginationNode);
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(_ref2, _ref3) {
      let {
        filteredRenderItems,
        pagination
      } = _ref2;
      let {
        current
      } = _ref3;
      const mergedPagination = parsePagination(pagination);
      if (mergedPagination) {
        // Calculate the page number
        const maxPageCount = Math.ceil(filteredRenderItems.length / mergedPagination.pageSize);
        if (current > maxPageCount) {
          return {
            current: maxPageCount
          };
        }
      }
      return null;
    }
  }]);
  return ListBody;
}(React.Component);
export default ListBody;